package com.hd.trans.framework.languages;

import android.view.View;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.entity.node.BaseNode;
import com.chad.library.adapter.base.provider.BaseNodeProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hd.trans.R;
import com.hd.trans.common.Constants;
import com.hd.trans.db.bean.HuDunLanguage;
import com.hd.trans.framework.dialog.DialogType;
import com.hd.trans.framework.dialog.LanguageDialogAdapter;

import org.greenrobot.greendao.annotation.NotNull;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author:  yang
 * Date:    2020/01/07 18:11
 * Desc:    类作用描述
 */
public class LanguageChildProvider extends BaseNodeProvider {
    DialogType dialogType;

    public LanguageChildProvider() {
        this.dialogType = DialogType.ONE;
    }

    public LanguageChildProvider(DialogType dialogType) {
        this.dialogType = dialogType;
    }

    @Override
    public int getItemViewType() {
        return Constants.LevelType.CHILD;
    }

    @Override
    public int getLayoutId() {
        int layoutId;
        if(DialogType.ONE == dialogType){
            layoutId = R.layout.item_language_child;
        }else if(DialogType.TWO == dialogType) {
            layoutId= R.layout.item_language_child2;
        }else {
            layoutId= R.layout.item_language_child;
        }
        return layoutId;
    }

    @Override
    public void convert(@NotNull BaseViewHolder helper, @Nullable BaseNode data) {
        // 数据类型需要自己强转
        final HuDunLanguage child = (HuDunLanguage) data;
        if (child != null) {
            helper.setText(R.id.tv_name, child.getName());
            if(DialogType.TWO == dialogType){
                if(!isHasChinese(child.getShortName()) || "ja".equals(child.getTranslateCode())){
                    helper.setText(R.id.tv_short_name, child.getShortName());
                }else {
                    helper.setText(R.id.tv_short_name, "");
                }
            }else {
                helper.setText(R.id.tv_short_name, "");
            }
            if (getAdapter() instanceof LanguageDialogAdapter) {
                LanguageDialogAdapter adapter = (LanguageDialogAdapter) getAdapter();
                if(adapter.getCheckedItem() != null){
                    boolean flag = child.getName().equals(adapter.getCheckedItem().getName());
                    helper.setVisible(R.id.iv_check, flag);
                    helper.getView(R.id.tv_name).setSelected(flag);
                    helper.getView(R.id.tv_short_name).setSelected(flag);
                }
            }
        }
    }

    /**
     * 是否包括中文
     * @param content
     * @return
     */
    public static boolean isHasChinese(String content) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(content);
        while(matcher.find()) {
            return true;
        }
        return false;
    }

    @Override
    public void onClick(@NotNull BaseViewHolder helper, @NotNull View view, BaseNode data, int position) {
//        getAdapter().expandOrCollapse(position);
        final HuDunLanguage child = (HuDunLanguage) data;
        if (child != null) {
            if (getAdapter() instanceof LanguageDialogAdapter) {
                LanguageDialogAdapter adapter = (LanguageDialogAdapter) getAdapter();
                adapter.setCheckedItem(child);
            }
        }
    }
}